/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricslocalprovider.h"
#include <QFileInfo>
#include "configmanager.h"

LyricsProvider* LyricsLocalProvider::Builder::create(const Track& track, QObject* parent) const {
    LyricsLocalProvider* inst = new LyricsLocalProvider(parent);
    inst->m_track = track;
    return inst;
};

const QString& LyricsLocalProvider::Builder::getProviderName() const {
    static QString name = LyricsLocalProvider::tr("Local file");
    return name;
};

void LyricsLocalProvider::search() {
    SearchResult result;
    QString basename = "/" + QFileInfo(m_track.url).completeBaseName() + ".lrc";
    ConfigManager* cfg = ConfigManager::getInstance();
    QStringList pathList = cfg->value("Lyrics/SearchPath",
        QStringList()).toStringList();
    pathList.prepend(QFileInfo(getFilename()).absolutePath());

    m_status = NotFound;
    for (int i = 0; i < pathList.size(); ++i) {
        QFileInfo f(pathList.at(i) + basename);
        if (f.exists() && f.isFile()) {
            m_status = Success;
            result.url = getFilename();
            m_result.push_back(result);
            break;
        };
    };
    emit signalSearched();
};

void LyricsLocalProvider::acceptResult(int) {
    emit signalReady();
};

